// site.isscloud.project.domain.PjStatsTemperatureMinute
package site.isscloud.project.domain;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
import java.math.BigDecimal;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

/**
 * 分钟温度统计对象 pj_stats_temperature_minute
 */
public class PjStatsTemperatureMinute {
    private static final long serialVersionUID = 1L;

    /** id */
    private Long id;

    /** 设备编号 */
    private String deviceNo;

    /** 通道号 */
    private Integer channelNo;

    /** 起始里程 */
    private Double startMeter;

    /** 结束里程 */
    private Double endMeter;

    /** 分钟最高温度数组 */
    private String maxTemperatures;

    /** 分钟最低温度数组 */
    private String minTemperatures;

    /** 分钟平均温度数组 */
    private String avgTemperatures;

    /** 统计时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date statTime;

    /** 样本数量 */
    private Integer sampleCount;

    /** 分钟最高温度Map (key: 数组下标+1, value: 温度值) */
    private Map<Integer, BigDecimal> maxTemperaturesMap;

    /** 分钟最低温度Map (key: 数组下标+1, value: 温度值) */
    private Map<Integer, BigDecimal> minTemperaturesMap;

    /** 分钟平均温度Map (key: 数组下标+1, value: 温度值) */
    private Map<Integer, BigDecimal> avgTemperaturesMap;

    // getter 和 setter 方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDeviceNo() {
        return deviceNo;
    }

    public void setDeviceNo(String deviceNo) {
        this.deviceNo = deviceNo;
    }

    public Integer getChannelNo() {
        return channelNo;
    }

    public void setChannelNo(Integer channelNo) {
        this.channelNo = channelNo;
    }

    public Double getStartMeter() {
        return startMeter;
    }

    public void setStartMeter(Double startMeter) {
        this.startMeter = startMeter;
    }

    public Double getEndMeter() {
        return endMeter;
    }

    public void setEndMeter(Double endMeter) {
        this.endMeter = endMeter;
    }

    public String getMaxTemperatures() {
        return maxTemperatures;
    }

    public void setMaxTemperatures(String maxTemperatures) {
        this.maxTemperatures = maxTemperatures;
    }

    public String getMinTemperatures() {
        return minTemperatures;
    }

    public void setMinTemperatures(String minTemperatures) {
        this.minTemperatures = minTemperatures;
    }

    public String getAvgTemperatures() {
        return avgTemperatures;
    }

    public void setAvgTemperatures(String avgTemperatures) {
        this.avgTemperatures = avgTemperatures;
    }

    public Date getStatTime() {
        return statTime;
    }

    public void setStatTime(Date statTime) {
        this.statTime = statTime;
    }

    public Integer getSampleCount() {
        return sampleCount;
    }

    public void setSampleCount(Integer sampleCount) {
        this.sampleCount = sampleCount;
    }

    public Map<Integer, BigDecimal> getMaxTemperaturesMap() {
        if(this.maxTemperatures != null && !this.maxTemperatures.isEmpty()
                && (this.maxTemperaturesMap == null || this.maxTemperaturesMap.isEmpty())) {
            this.processTemperatureData();
        }
        return maxTemperaturesMap;
    }

    public void setMaxTemperaturesMap(Map<Integer, BigDecimal> maxTemperaturesMap) {
        this.maxTemperaturesMap = maxTemperaturesMap;
    }

    public Map<Integer, BigDecimal> getMinTemperaturesMap() {
        if(this.minTemperatures != null && !this.minTemperatures.isEmpty()
                && (this.minTemperaturesMap == null || this.minTemperaturesMap.isEmpty())) {
            this.processTemperatureData();
        }
        return minTemperaturesMap;
    }

    public void setMinTemperaturesMap(Map<Integer, BigDecimal> minTemperaturesMap) {
        this.minTemperaturesMap = minTemperaturesMap;
    }

    public Map<Integer, BigDecimal> getAvgTemperaturesMap() {
        if(this.avgTemperatures != null && !this.avgTemperatures.isEmpty()
                && (this.avgTemperaturesMap == null || this.avgTemperaturesMap.isEmpty())) {
            this.processTemperatureData();
        }
        return avgTemperaturesMap;
    }

    public void setAvgTemperaturesMap(Map<Integer, BigDecimal> avgTemperaturesMap) {
        this.avgTemperaturesMap = avgTemperaturesMap;
    }

    /**
     * 处理温度数据，将字符串数组转换为Map
     */
    public void processTemperatureData() {
        this.maxTemperaturesMap = convertStringToMap(this.maxTemperatures);
        this.minTemperaturesMap = convertStringToMap(this.minTemperatures);
        this.avgTemperaturesMap = convertStringToMap(this.avgTemperatures);
    }

    /**
     * 将温度字符串转换为Map
     * @param temperatureString 温度字符串，格式如："1.2,2.3,3.4"
     * @return Map<Integer, BigDecimal> key为下标+1，value为温度值
     */
    private Map<Integer, BigDecimal> convertStringToMap(String temperatureString) {
        Map<Integer, BigDecimal> resultMap = new HashMap<>();
        if (temperatureString == null || temperatureString.trim().isEmpty()) {
            return resultMap;
        }

        // 多种清理策略
        String cleanedString = temperatureString
                .replaceAll("[\\[\\]]", "")  // 移除所有方括号
                .replaceAll("\\s+", ",")     // 将所有空白字符（包括换行、空格等）替换为逗号
                .replaceAll(",+", ",")       // 将多个连续逗号合并为一个
                .replaceAll("^,|,$", "")     // 移除首尾的逗号
                .trim();

        String[] temperatureArray = cleanedString.split(",");
        int validIndex = 0;

        for (String tempStr : temperatureArray) {
            String trimmedTemp = tempStr.trim();
            if (!trimmedTemp.isEmpty()) {
                try {
                    BigDecimal temperature = new BigDecimal(trimmedTemp);
                    resultMap.put(validIndex + 1, temperature);
                    validIndex++;
                } catch (NumberFormatException e) {
                    System.err.println("温度值转换失败: '" + trimmedTemp + "' (原始: '" + temperatureString + "')");
                }
            }
        }

        return resultMap;
    }

    /**
     * 批量处理分钟统计列表，将字符串温度数据转换为Map
     * @param minuteStatsList 从数据库获取的分钟统计列表
     */
    public static void processMinuteStatsList(List<PjStatsTemperatureMinute> minuteStatsList) {
        if (minuteStatsList == null) {
            return;
        }

        for (PjStatsTemperatureMinute stats : minuteStatsList) {
            stats.processTemperatureData();
        }
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", getId())
                .append("deviceNo", getDeviceNo())
                .append("channelNo", getChannelNo())
                .append("startMeter", getStartMeter())
                .append("endMeter", getEndMeter())
                .append("maxTemperatures", getMaxTemperatures())
                .append("minTemperatures", getMinTemperatures())
                .append("avgTemperatures", getAvgTemperatures())
                .append("statTime", getStatTime())
                .append("sampleCount", getSampleCount())
                .append("maxTemperaturesMap", getMaxTemperaturesMap())
                .append("minTemperaturesMap", getMinTemperaturesMap())
                .append("avgTemperaturesMap", getAvgTemperaturesMap())
                .toString();
    }
}